package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.po.AdminLogPO;
import com.cskaoyan.service.AdminService;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.tomcat.util.http.fileupload.FileUploadBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import javax.naming.SizeLimitExceededException;
import javax.servlet.http.HttpServletRequest;

/**
 * 该异常适配  402;
 *
 * @author: DYQ
 * @time: Created on 2022/6/6 23:12
 */
@RestControllerAdvice
public class CustomControllerAdvice {

    @Autowired
    AdminService adminService;
    /**
     * message 该方法处理异常402 ,参数值异常
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public BaseRespVo methodArgumentTypeMismatchExceptionHandle(MethodArgumentTypeMismatchException exception) {
        String message = exception.getMessage();

//        String word = exception.getWord();
//        message = message + ":" + word;
//        BaseRespVo baseRespVo = new BaseRespVo();
////        baseRespVo.setMsg(message);
////        baseRespVo.setStatus(400);
//        return baseRespVo;
        return BaseRespVo.err("参数值不对", 402);
    }

//    private final static Logger logger = LoggerFactory.getLogger(CustomControllerAdvice.class);


    /**
     * 该方法处理上传图片过大的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = MultipartException.class)
    @ResponseBody
    public String multipartExceptionHandler(MaxUploadSizeExceededException e) {
        String errorMsg = "文件gg";
        Throwable throwable = e.getCause();
//        logger.error(e.getMess  age());
        // org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException
        if (throwable.getCause() instanceof FileUploadBase.FileSizeLimitExceededException) {
            errorMsg = throwable.getCause().getMessage();
        }
        // org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException
        if (throwable.getCause() instanceof SizeLimitExceededException) {
            errorMsg = throwable.getCause().getMessage();
        }
        return "文件gg";
    }


    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseBody
    public BaseRespVo login(IncorrectCredentialsException e) {
        AdminLoginWrong();
        return BaseRespVo.noLogin("账户名或密码错误!");
    }

    private void AdminLoginWrong() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        AdminLogPO adminLogPO = (AdminLogPO) request.getSession().getAttribute("loginWrong");
        adminService.insertAdminLog(adminLogPO);
    }

    @ExceptionHandler(UnknownAccountException.class)
    @ResponseBody
    public BaseRespVo login(UnknownAccountException e) {
        AdminLoginWrong();
        return BaseRespVo.noLogin("账户不存在!");
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public BaseRespVo nullPoint(UnknownAccountException e) {
        return BaseRespVo.noLogin("还没登录哟!");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public BaseRespVo goodsException(HttpMessageNotReadableException e) {
        return BaseRespVo.err("参数错误", 701);
    }

//    @ExceptionHandler(Exception.class)
//    @ResponseBody
//    @Order(Ordered.LOWEST_PRECEDENCE)
//    public BaseRespVo goodsException(Exception e) {
//        return BaseRespVo.err("权限不足", 701);
//    }


}
